/**
 * 
 */
package com.kun.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;


/**
 * 生成静态页面
 * @author KM
 * 2018年3月16日
 */
public class StaticPageGenerateUtil {
	
	/*
	 * @parms modelPage 模板页面
	 * @parms resultPage 目标静态页面
	 * @parms map 模型数据
	 * */
	public static String generatePage(String modelPage,String resultPage,Context context){
		String msg = "";
		//构造模板引擎
        ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
        resolver.setPrefix("templates/");//模板所在目录，相对于当前classloader的classpath。
        resolver.setSuffix(".html");//模板文件后缀
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(resolver);

        //构造上下文(Model)
//        Context context = new Context();
//        Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
//        while (it.hasNext()) {
//	        Map.Entry<String, Object> entry = it.next();
//	        context.setVariable(entry.getKey(),(User)entry.getValue());
//        }
		//context.setVariable("name", "蔬菜列表");
		//context.setVariable("array", new String[]{"土豆", "番茄", "白菜", "芹菜"}); 
		try {
			//渲染模板
			File f = new File("src//main//resources//static//page//" + resultPage+".html");
			FileWriter	write = new FileWriter(f);
			templateEngine.process(modelPage, context, write);
			msg = "成功:" + new Date();
			write.close();
		} catch (IOException e) {
			msg = "失败:" + new Date();
			e.printStackTrace();
		}
		return msg;
	}

}
